package cn.hz.pi.api.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StringUtil {
	public static final Integer convertStringToInteger(String str) {
		Integer ret = null;
		try {
			ret = Integer.parseInt(str);
		} catch (Exception e) {
			ret = null;
		}
		return ret;
	}

	public static final Long convertStringToLong(String str) {
		Long ret = null;
		try {
			ret = Long.valueOf(str);
		} catch (Exception e) {
			ret = null;
		}
		return ret;
	}

	public static boolean isNull(String str) {
		return null == str || str.length() == 0;
	}

	public static boolean isEnNum(String str) {
		if (isNull(str))
			return false;

		if (str.matches("^[0-9]+$"))
			return false;

		return str.matches("^[a-zA-Z0-9]+$");
	}

	public static String toString(Object obj) {
		if (null == obj)
			return null;

		return String.valueOf(obj);
	}

	public static float[] convertToFloatS(String str, String splitStr) {
		if (isNull(str)) {
			return null;
		}

		String[] strS = str.split(",");
		if (null == strS || strS.length == 0)
			return null;
		float[] fs = new float[strS.length];
		for (int i = 0; i < strS.length; i++) {
			fs[i] = convertToFloat(strS[i]);
		}

		return fs;
	}

	public static float convertToFloat(String str) {
		float f = 0F;
		try {
			f = Float.valueOf(str).floatValue();
		} catch (Exception e) {
			f = 0F;
		}

		return f;
	}

	public static Float toFloat(Object object) {
		Float retValue = null;
		try {
			retValue = Float.valueOf(String.valueOf(object));
		} catch (Exception e) {
			retValue = null;
		}

		return retValue;
	}

	// 求最大
	public static Float maxValue(List<Float> list) {
		if (null == list || list.isEmpty()) {
			return null;
		}
		try {
			return Collections.max(list);
		} catch (Exception e) {
		}
		return null;
	}

	// 求最小
	public static Float minValue(List<Float> list) {
		if (null == list || list.isEmpty()) {
			return null;
		}
		try {
			return Collections.min(list);
		} catch (Exception e) {
		}
		return null;
	}

	// 求差和
	public static Float sumValue(List<Float> list) {
		if (null == list || list.isEmpty()) {
			return null;
		}

		Float tagVal = 0f;
		for (Float f : list) {
			tagVal += f.floatValue();
		}
		return tagVal;
	}

	// 求时间加权平均
	public static Float avgValue(List list) {
		if (null == list || list.isEmpty()) {
			return null;
		}

		Float tagVal = 0f;
		Float sum = 0f;
		Long ax = 0l;
		for (int i = 0; i < list.size(); i++) {
			List subList = (List) list.get(i);
			if (null != subList && subList.size() == 2) {
				Float f = (Float) subList.get(0);
				Long l = (Long) subList.get(1);
				sum += f * l;
				ax += l;
			}
		}
		tagVal = sum / ax;
		return tagVal;
	}

	public static void main(String[] args) {
		List<List> list = new ArrayList<List>();
		List subList = new ArrayList();
		subList.add(2f);
		subList.add(2l);
		list.add(subList);

		subList = new ArrayList();
		subList.add(3f);
		subList.add(4l);
		list.add(subList);

		avgValue(list);
	}

//	public static String getKey(Object... key) {
//		if (null == key)
//			return null;
//		try {
//			return Joiner.on(":").useForNull("").join(key);
//		} catch (Exception e) {
//		}
//		return null;
//	}
}
